﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
using System.IO;
using Microsoft.Win32;

using Pg.BioMedics.SDR.Client.OperationalWebService;
using Pg.BioMedics.SDR.Client.Commands;

namespace Pg.BioMedics.SDR.Client
{
    /// <summary>
    /// Interaction logic for RegisterDocumentWindow.xaml
    /// </summary>
    public partial class RegisterDocumentWindow : Window
    {
        #region Fields

        private Document document;
        private Content content;

        #endregion

        #region Constructors

        public RegisterDocumentWindow()
        {
            InitializeComponent();
            InitializeDataContext();
        }

        #endregion 
        
        #region Properties

        internal Document Document
        {
            get
            {
                return this.document;
            }
        }

        internal Content Content
        {
            get
            {
                return this.content;
            }
        }

        #endregion

        #region Event handlers

        private void SelectContentButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // select file
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.Filter = "Images|*.jpg|Audio|*.wav|Other|*.*";
                bool? result = openFileDialog.ShowDialog(this);
                if (!result.HasValue)
                    return;
                else if (!result.Value)
                    return;

                // check extension
                if (System.IO.Path.GetExtension(openFileDialog.FileName) == ".jpg")
                {
                    this.document.TypeCode = "image";
                    this.content.TypeCode = "jpeg";
                }
                else if (System.IO.Path.GetExtension(openFileDialog.FileName) == ".wav")
                {
                    this.document.TypeCode = "audio";
                    this.content.TypeCode = "wav";
                }
                else
                {
                    this.document.TypeCode = "other";
                    this.content.TypeCode = "raw";
                }

                // store file name
                this.content.FileName = openFileDialog.FileName;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #region Command methods

        #region Register

        private void RegisterCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if ((this.document == null) || (this.content == null))
                e.CanExecute = false;
            else if (String.IsNullOrEmpty(this.document.Title))
                e.CanExecute = false;
            else if (String.IsNullOrEmpty(this.content.FileName))
                e.CanExecute = false;
            else
                e.CanExecute = true;
        }

        private void RegisterCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        #endregion

        #region Cancel

        private void CancelCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            this.document = null;
            this.content = null;

            DialogResult = false;
            Close();
        }

        #endregion

        #endregion

        #region Support methods

        private void InitializeDataContext()
        {
            this.document = new Document() { LanguageCode = CultureInfo.CurrentUICulture.ThreeLetterISOLanguageName };
            this.content = new Content();

            DataContext = new { Document = document, Content = content };
        }

        #endregion              
    }
}
